package com.hxh.generater.springBoot.springboot3_vue3.gen;


import com.hxh.generater.springBoot.util.ProcessWriteLine;
import com.hxh.entity.Project;
import com.hxh.util.GenUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class ReplaceGroupIdUtil {

    /**
     * 替换GroupId
     *
     * @param templatePath 相对路径
     * @param classFile    文件
     * @param project      项目
     */
    public static void replaceUtil(String templatePath, File classFile, Project project) {
        try {
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(GenSb3Vue3Util.FILE_PATH_PRE + templatePath);
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            Map<String, String> replaceWord = new HashMap<>(1);
            String projectGroup = project.getBackendProject().getProjectGroup();
            String GroupIdSlash = projectGroup;
            if (projectGroup.contains(".")) {
                GroupIdSlash = projectGroup.replace(".", "/");
            }
            replaceWord.put("GroupId", projectGroup);
            replaceWord.put("GroupIdSlash", GroupIdSlash);
            ProcessWriteLine processWriteLine = new ProcessWriteLine(replaceWord);
            while ((line = br.readLine()) != null) {
                processWriteLine.writeLine(line, writer);
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 替换GroupId
     *
     * @param templatePath  模板位置
     * @param classFilePath 生成代码位置
     * @param project       项目
     */
    public static void replaceUtil(String templatePath, String classFilePath, String fileName, Project project) {
        try {
            File classFile = GenUtil.newFile(project, classFilePath, fileName);
            replaceUtil(templatePath, classFile, project);
        } catch (IOException e) {
            throw new RuntimeException("初始化" + classFilePath + "/" + fileName + "异常!");
        }
    }

    /**
     * 替换文件夹内所有文件的GroupId，按照文件夹内的相对目录，在对应的项目的相对路径中创建对应文件
     *
     * @param templateDirPath 文件夹
     * @param project         项目
     */
    public static void replaceDirectory(String templateDirPath, Project project, String type) {
        File file = new File(GenSb3Vue3Util.FILE_PATH_PRE + templateDirPath);
        if (!file.exists()) {
            throw new RuntimeException("初始化" + templateDirPath + "异常!");
        }
        File[] files = file.listFiles();
        if (files == null) {
            return;
        }
        for (File file1 : files) {
            if (file1.isDirectory()) {
                replaceDirectory(templateDirPath + "\\" + file1.getName(), project, type);
                continue;
            }
            String fileName = file1.getName();
            if (fileName.endsWith("_Redis")) {
                continue;
            }
            if (fileName.endsWith("_Mongo")) {
                continue;
            }
            if (fileName.endsWith("_Docker")) {
                continue;
            }
            String filePath = file1.getPath();
            filePath = filePath.replace(GenSb3Vue3Util.FILE_PATH_PRE, "");
            if (StringUtils.isBlank(type)) {
                String classFilePathPrefix = filePath.substring(0, filePath.length() - fileName.length() - 1);
                replaceUtil(filePath, classFilePathPrefix, fileName, project);
            } else if ("mapper".equals(type)) {
                replaceUtil(filePath, "mapper", fileName, project);
            }

        }
    }

    public static void replaceMapperXmlDirectory(String templateDirPath, Project project) {
        replaceDirectory(templateDirPath, project, "mapper");
    }

    public static void replaceDirectory(String templateDirPath, Project project) {
        replaceDirectory(templateDirPath, project, null);
    }

}
